package com.study_security.controller;
import com.study_security.domain.LoginUser;
import com.study_security.entity.Project;
import com.study_security.service.ExportService;
import com.study_security.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/export")
public class ExportController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ExportService exportService;

    /**
     * 审核用的，可以看到所有的项目
     *
     * @return
     * @throws IOException
     */
    @PreAuthorize("hasAuthority('perm_review')")
    @GetMapping("/all/excel")
    public ResponseEntity<byte[]> exportProjectsToExcel() throws IOException {
        List<Project> projects = projectService.getAllProjects();
        byte[] data = exportService.exportProjectsToExcel(projects);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", "project.xlsx");

        return ResponseEntity.ok().headers(headers).body(data);
    }

    /**
     * 用户用的（项目负责人）
     * @param authentication
     * @return
     * @throws IOException
     */
    @PreAuthorize("hasAuthority('perm_leader')")
    @GetMapping("/user/excel")
    public ResponseEntity<byte[]> exportProjectsToExcel(Authentication authentication) throws IOException {
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Long userId = loginUser.getUser().getId();
        String userName=loginUser.getUser().getUserName();
        List<Project> projects = projectService.getAllProjectByUserId(userId);
        byte[] data = exportService.exportProjectsToExcel(projects);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", userName+"-"+"project.xlsx");

        return ResponseEntity.ok().headers(headers).body(data);
    }

//    @GetMapping("/all/excel")
//    public ResponseEntity<byte[]> exportProjectsToExcelByUser(Authentication authentication) throws IOException {
//        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
//        Long userId = loginUser.getUser().getId();
//        List<Project> projects = projectService.getAllProject();
//        byte[] data = exportService.exportProjectsToExcel(projects);
//
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//        headers.setContentDispositionFormData("attachment", "project.xlsx");
//
//        return ResponseEntity.ok().headers(headers).body(data);
//    }


//    @GetMapping("/pdf")
//    public ResponseEntity<byte[]> exportProjectsToPDF() throws IOException {
//        List<Project> projects = projectService.getAllProjects(); // 假设有这个方法
//        byte[] data = exportService.exportProjectsToPDF(projects);
//
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_PDF);
//        headers.setContentDispositionFormData("attachment", "projects.pdf");
//
//        return ResponseEntity.ok().headers(headers).body(data);
//    }
}

